package com.fmn.dots_final_i_hope;

import model.Line;
import model.Player;

public class PlayerControl {

	/**
	 * This class creates 2 Player objects. Has two main functionalities - to
	 * change the current player and to know which player's turn is
	 */

	public static Player p1 = new Player("PLAYER 1", R.drawable.taken_square1);
	public static Player p2 = new Player("PLAYER 2", R.drawable.taken_square2);
	public static int choosenLayoutId = R.layout.field_3x3;
	static int choosenRow = 3, choosenCol = 3;
	static int maxPointsPossible;

	public static void setMaxPointsPossible() {
		maxPointsPossible = choosenRow * choosenCol;
	}

	public static void doAPlayerTurn(Line line) {
		if (line.isPressed() == false) {
			line.setPressed(true);

			if (line.tryToObtainSquare() == false)
				changePlayer();
		}
	}

	public static void setPlayerNames(String p1Name, String p2Name) {
		p1.setName(p1Name);
		p2.setName(p2Name);
	}

	public static String anounceWinner() {
		if (p1.getCurrentPoints() > p2.getCurrentPoints())
			return p1.getName() + " WON ! ! !";
		if (p2.getCurrentPoints() > p1.getCurrentPoints())
			return p2.getName() + " WON ! ! !";

		return "The game is draw!";
	}

	public static void nullStaticVariables() {
		setMaxPointsPossible();
		p1.setCurrentPoints(0);
		p2.setCurrentPoints(0);
	}

	public static boolean checkForEndGame() {
		if (p1.getCurrentPoints() + p2.getCurrentPoints() >= maxPointsPossible) {
			return true;
		}
		return false;
	}

	static Player currentPlayer = p1;

	public static void changePlayer() {
		if (currentPlayer == p1) {
			currentPlayer = p2;
		} else {
			currentPlayer = p1;
		}
	}

	public static Player getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(Player currentPlayer) {
		PlayerControl.currentPlayer = currentPlayer;
	}

}
